import { Toast, Dialog } from 'vant'
import { loginOut, getNowRouter } from './com'
import { getLocal } from './cache'
export default {
	methods: {
		formatWebMethod(action,param){
			let _this = this
			switch (action) {
				// 互踢功能
				case "offline":
				_this.offline(action,param)
				break
				// 公告推送
				case "notice":
				_this.notice(action,param)
				break
				// 单击返回上一页
				case "goBack":
				_this.goBack(action,param)
				break
				// 双击退出程序
				case "exitApp":
				_this.exitApp(action,param)
				break
				// 默认
				default:
				_this.errors(action,param)
				break
			}
		},
		// 互踢功能
		offline(action,param){
			loginOut(action,param)
		},
		// 公告推送
		notice(action,param){
			let id = null,
				data = null,
				_this = this;
			if(param && param.content){
				data = JSON.parse(param.content)
			}
			if(data && data.ggId){
				id = data.ggId
			}
			if(id){
				let query = {
					id: id
				}
				_this.$router.push({
					name: 'gonggao_detail',
					query: query
				})
			}else{
				Toast('公告内容获取失败！')
			}
		},
		// 单击返回上一页
		goBack(action,param){
			this.$router.go(-1)
//			let router = getNowRouter()
//			let getLocals = JSON.parse(getLocal(router))
//			console.log("单击返回上一页.",getLocals.router)
//			this.$router.push({
//				name: getLocals.router,
//				query: getLocals.query,
//				params: {
//					goBack: true
//				}
//			})
		},
		// 双击退出程序
		exitApp(action,param){
			if(!window.Cordova){
				return
			}
			if(window.location.hash.includes('/login') || window.location.hash.includes('/index')){
				navigator.app.exitApp()
			}else{
				this.goBack(action,param)
			}
		},
		// 默认错误调取
		errors(action,param){
			console.error("安卓调用方法出错..")
		},
		// 每个页面重新赋值，避免页面this混淆
		getWebThis(){
			window.webMethod = this.formatWebMethod
			window.thisWeb = this
		}
	},
    mounted () {
    		this.getWebThis()
    },
    watch: {
        '$route' (newRoute) {
        		this.getWebThis()
        }
    }
}